import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;


public class CommunityChestUI extends JFrame {

	private JPanel contentPane;
	private JLabel imageLabel;
	private ImageIcon image;
	
	private int number;
	private int position;
	private Scanner scanner;
	private Cards[] cards;
	private Player player;
	private Block[] bl;
	private Board b;
	private Player[] pArray;
	private PropertyBlock[] propBlkTemp;
	private PropertyBlock[] propertyBlk;
	private Random random;
	private Player[] playerArray;
	
	private static CommunityChestUI singleCommunityChestUI;
	
	protected CommunityChestUI(Player player, Block[] blocks, Player[] playerArray) {
		
		this.player = player;
		this.bl = blocks;
		this.cards = new Cards[16];
		this.playerArray = playerArray;
		ArrayList<PropertyBlock> propertyBlockArrayList = new ArrayList<PropertyBlock>();
		for(int i=0; i<bl.length;i++){//get number of property blocks
			if(this.bl[i] instanceof PropertyBlock){
				propertyBlockArrayList.add((PropertyBlock) this.bl[i]);
			}
		}
		
		//propBlkTemp = new PropertyBlock[PropCountTotal];//create array of propertyBlocks
		
		this.propertyBlk = new PropertyBlock[propertyBlockArrayList.size()];
		propertyBlockArrayList.toArray(this.propertyBlk);
		
		propertyBlockArrayList.clear();
		
		for(int i=0; i<this.propertyBlk.length; i++){
			
			if(this.propertyBlk[i].getCOwner()==player){
				
				propertyBlockArrayList.add(this.propertyBlk[i]);
				
			}
			
		}
		
		this.propertyBlk = new PropertyBlock[propertyBlockArrayList.size()];
		propertyBlockArrayList.toArray(this.propertyBlk);
		
		
		
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(10, 10, 723, 557);
		contentPane = new JPanel();
		
		setContentPane(contentPane);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setVisible(true);
		contentPane.setLayout(null);
		
		this.openFile();
		this.readFile();
		this.closeFile();	
	}

	
	public static CommunityChestUI getInstance(Player play, Block[] blks, Player[] pArray){
		  if(singleCommunityChestUI==null){
			  singleCommunityChestUI=new CommunityChestUI(play, blks, pArray);
		  }
		  
		  return singleCommunityChestUI;
	  }
	
	private void exitButtonPressed(ActionEvent extEvent) {
		this.execute();
		this.dispose();
	
	}
	
	public void drawCard(){
		
		random = new Random();
		this.number=random.nextInt(16);
		
		if(number == 4 && cards[4].getPlayer().getPlayerName() != null){
			random = new Random();
			this.number=random.nextInt(16);
		}
		
		this.image = new ImageIcon(cards[number].getCardImage());
		this.setTitle(cards[number].getCardType());
		
		if(this.imageLabel!=null){
			this.contentPane.setVisible(false);
			this.imageLabel.setVisible(false);
			this.contentPane.remove(imageLabel);
		}
		
		this.imageLabel = new JLabel(this.image);
		this.imageLabel.setBackground(Color.WHITE);
		this.imageLabel.setBounds(8, 5, 690, 450);
		this.imageLabel.setVisible(true);
		contentPane.add(this.imageLabel);
		
		System.out.println(image);
			
		JButton exitButton = new JButton("Okay");
		exitButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent extEvent) {
				exitButtonPressed(extEvent);
			}

		});
		
		exitButton.setBounds(310, 466, 116, 30);
		contentPane.add(exitButton);	
		
		setContentPane(contentPane);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setVisible(true);
		contentPane.setLayout(null);
	}
	
	public void openFile(){
		try{
			scanner = new Scanner(new File("src/community.txt"));
			scanner.useDelimiter(",");
		}catch(Exception e){
			System.out.println("Error Reading File");
		}
	}

	public void readFile(){
		int counter=0;
		do{
			
				if(counter == 0 || counter == 5){
					this.cards[counter]= new Cards(scanner.next(),Integer.parseInt(scanner.next()), scanner.next());
				}else if(counter == 12){
					this.cards[counter] = new Cards(scanner.next(),Integer.parseInt(scanner.next()), scanner.next(), Integer.parseInt(scanner.next()), Integer.parseInt(scanner.next()));
				}else if(counter == 4){
					this.cards[counter] = new Cards(scanner.next(),Integer.parseInt(scanner.next()), scanner.next(), this.player);
				}else{
					this.cards[counter] = new Cards(scanner.next(),Integer.parseInt(scanner.next()), scanner.next(), Integer.parseInt(scanner.next()));				
				}
				
				counter++;
			
		}while(scanner.hasNext());
		
	}
	
	
	public void closeFile(){
		scanner.close();
	}
	
	
	public void execute(){
		
		position = player.getCurrentBlockPos().getBlockNumber();
		Command moveCmd = new MovePlayerCommand();
		Command nextCmd = new NextPlayerCommand();
		Command jailCmd = new SendCurrentPlayerToJailCommand();
		CommandWithReturn bankruptcyCmd = new BankruptcyCheckerCommand();
		CommandWithInt cmdInt = new PropertySelectionUICommand();
		Command transferCmd = new TransferAssetsCommand();
		
		switch(number){
		
			case 0: 			
				
				if(position == 2){
					player.setLastRoll(38);
				}else if(position == 17){
					player.setLastRoll(23);
				}else if(position == 33){
					player.setLastRoll(7);
				}
				
				moveCmd.execute();			
				break;
				
			case 1:
				
				player.addMoney(cards[1].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[1].getAmount() + "Php. Ang pera mo ngayon ay " + player.getCurrentMoney() + "Php.");
				nextCmd.execute();
				break;
				
			case 2:
				
				if(player.getCurrentMoney()<cards[2].getAmount()){
					bankruptcyCmd.execute();
					if(bankruptcyCmd.execute() == 0){
						JOptionPane.showMessageDialog(null, "Wala ka nang pera kahit ibenta mo pa lahat ng pag-aari mo. Natalo ka sa laro, sir.");
						transferCmd.execute();
						this.player.setActive(false);
					}else{
						JOptionPane.showMessageDialog(null, "Wala ka nang pera pero pwede mong ibenta ang mga pag-aari mo.");
						cmdInt.execute(2);
					}
				}else{
					player.subtractMoney(cards[2].getAmount());
					JOptionPane.showMessageDialog(null, "Nagbayad ka ng " + cards[2].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				}
				nextCmd.execute();
				break;
				
			case 3:
				
				player.addMoney(cards[3].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[3].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				nextCmd.execute();				
				break;
				
			case 4: 
				
				player.setCommunityJailToken(1);
				nextCmd.execute();
				break;
				
			case 5:
				
				jailCmd.execute();
				nextCmd.execute();
				break;
				
			case 6:
				
				player.addMoney(cards[6].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[6].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				nextCmd.execute();
				break;
				
			case 7: 
				if(this.playerArray.length>1){
					int totalPlayerPayment = (this.playerArray.length-1) * cards[15].getAmount();
					for(int counter = 0; counter<this.playerArray.length; counter++){
						if(this.playerArray[counter] != this.player){
						}
					}
					
					player.addMoney(totalPlayerPayment);
					JOptionPane.showMessageDialog(null, "Kumita ka ng " + totalPlayerPayment + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
					nextCmd.execute();
				}else{
					JOptionPane.showMessageDialog(null, "Wala kang ibang kalaro. Ang pera mo ay " + player.getCurrentMoney() +"Php pa rin.");
					nextCmd.execute();
				}
				
				break;
				
			case 8:
				
				player.addMoney(cards[8].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[8].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				nextCmd.execute();
				break;
				
			case 9:
				
				if(player.getCurrentMoney()<cards[9].getAmount()){
					bankruptcyCmd.execute();
					if(bankruptcyCmd.execute() == 0){
						JOptionPane.showMessageDialog(null, "Wala ka nang pera kahit ibenta mo pa lahat ng pag-aari mo. Natalo ka sa laro, sir.");
						transferCmd.execute();
						this.player.setActive(false);
					}else{
						JOptionPane.showMessageDialog(null, "Wala ka nang pera pero pwede mong ibenta ang mga pag-aari mo.");
						cmdInt.execute(2);
					}
				}else{
					player.subtractMoney(cards[9].getAmount());
					JOptionPane.showMessageDialog(null, "Nagbayad ka ng " + cards[9].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				}
				nextCmd.execute();
				break;
				
			case 10:

				if(player.getCurrentMoney()<cards[10].getAmount()){
					bankruptcyCmd.execute();
					if(bankruptcyCmd.execute() == 0){
						JOptionPane.showMessageDialog(null, "Wala ka nang pera kahit ibenta mo pa lahat ng pag-aari mo. Natalo ka sa laro, sir.");
						transferCmd.execute();
						this.player.setActive(false);
					}else{
						JOptionPane.showMessageDialog(null, "Wala ka nang pera pero pwede mong ibenta ang mga pag-aari mo.");
						cmdInt.execute(2);
					}
				}else{
					player.subtractMoney(cards[10].getAmount());
					JOptionPane.showMessageDialog(null, "Nagbayad ka ng " + cards[10].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				}
				nextCmd.execute();
				break;
				
			case 11:
				
				player.addMoney(cards[11].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[11].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				nextCmd.execute();
				break;
				
			case 12:
				
				int houses = 0;
				int hotels = 0;
				int totalHotelAmount;
				int totalHouseAmount;
				
				for(int counter = 0; counter<this.propertyBlk.length; counter++){
					if(propertyBlk[counter].getCurrentNumberOfHouses() == 5){
						hotels = hotels+1;
					}else{
						houses = houses+propertyBlk[counter].getCurrentNumberOfHouses();
					}
				}
				
				if(houses != 0 && hotels !=0){
					totalHouseAmount = houses * cards[12].getAmount();
					totalHotelAmount = hotels * cards[12].getHotelAmount();
					int totalPayment = totalHouseAmount+totalHotelAmount;
					
					if(player.getCurrentMoney()<totalHouseAmount || player.getCurrentMoney()<totalHotelAmount){
						bankruptcyCmd.execute();
						if(bankruptcyCmd.execute() == 0){
							JOptionPane.showMessageDialog(null, "Wala ka nang pera kahit ibenta mo pa lahat ng pag-aari mo. Natalo ka sa laro, sir.");
							transferCmd.execute();
							this.player.setActive(false);
						}else{
							JOptionPane.showMessageDialog(null, "Wala ka nang pera pero pwede mong ibenta ang mga pag-aari mo.");
							cmdInt.execute(2);
						}
					}else{
						player.subtractMoney(totalPayment);
						JOptionPane.showMessageDialog(null, "Nagbayad ka ng " + totalPayment + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
					}
					nextCmd.execute();
				}else{
					JOptionPane.showMessageDialog(null, "Wala ka pang mga bahay at hotel. Ang pera mo ay " + player.getCurrentMoney() + "Php pa rin.");
					nextCmd.execute();
				}
				break;
				
			case 13:
				
				player.addMoney(cards[13].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[13].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				nextCmd.execute();
				break;
				
			case 14:
				
				player.addMoney(cards[14].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[14].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");	
				nextCmd.execute();
				break;
				
			case 15:
				
				player.addMoney(cards[15].getAmount());
				JOptionPane.showMessageDialog(null, "Kumita ka ng " + cards[15].getAmount() + "Php. Ang pera mo na ngayon ay " + player.getCurrentMoney() + "Php.");
				nextCmd.execute();
				break;

		}
	}
}
